//
//  XNSearchBar.swift
//  weibo
//
//  Created by 王向宁 on 16/5/20.
//  Copyright © 2016年 王向宁. All rights reserved.
//

import UIKit

class XNSearchBar: UIView {
    @IBOutlet weak var cancelBtn: UIButton!

    @IBOutlet weak var searchTF: UITextField!
   
    @IBOutlet weak var rightcons: NSLayoutConstraint!

    @IBAction func cancleBtnDidClick(sender: AnyObject) {
        
        self.rightcons.constant = 0
        
        UIView.animateWithDuration(0.25) {
            self.layoutIfNeeded()
        }
        
        self.endEditing(true)
    }
    @IBAction func textFiledBeginEditing(sender: AnyObject) {
        
        UIView.animateWithDuration(0.25) {
            self.rightcons.constant = self.cancelBtn.bounds.width
            
            self.layoutIfNeeded()
        }
    }
    
    //提供加载nib的类方法
    class func loadSearchBar() -> XNSearchBar {
        let searchView = NSBundle.mainBundle().loadNibNamed("XNSearchBar", owner: nil, options: nil).last as! XNSearchBar
        return searchView
    }
    
    //在awakeFromNib做初始设置
    override func awakeFromNib() {
        self.bounds.size.width = UIScreen.mainScreen().bounds.width
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        searchTF.leftViewMode = .Always
        
        searchTF.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        
        searchTF.leftView?.contentMode = .Center
    }
}
